package com.cysion.study.widget.downmenu

import android.content.Context
import android.opengl.Visibility
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.cysion.study.R
import kotlinx.android.synthetic.main.popup_menu.view.*


/**
 * Created by chu on 2019-07-14.
 */
class SimpleDropDownMenuAdapter(private var context:Context,private val itemList: MutableList<MenuItem>) : RecyclerView.Adapter<SimpleDropDownMenuAdapter.VH>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_menu, parent, false)
        return VH(view)

    }
    private var isShow = false
    private var isShowChild = false

    //子列表
//    var mMenuItemList = mutableListOf<MenuItem>()
//    var mMenuItemAdapter: ChildMenuAdapter? = null


    override fun onBindViewHolder(holder: VH, position: Int) {
        holder?.itemTitleTextView?.text = itemList[position].itemTitle
        if (isShow) {
//        holder?.itemIconImgView?.imageResource = itemList[position].itemIcon
            holder.itemIconImgView.visibility = View.VISIBLE
        } else {
            holder.itemIconImgView.visibility = View.GONE
        }

       /* if (isShowChild) {
            holder.child_item_list.visibility = View.VISIBLE
            mMenuItemAdapter = ChildMenuAdapter(mMenuItemList)
            //mMenuItemAdapter?.itemClickListener = this
            holder.child_item_list.layoutManager =
                LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            holder.child_item_list.adapter = mMenuItemAdapter
        } else {
            holder.child_item_list.visibility=View.GONE
        }
*/
        holder?.itemLinear?.setOnClickListener {
            if (isShowChild) {

            } else {

                itemClickListener?.onItemClick(holder.itemLinear,position)
            }
        }
    }

    var itemClickListener: ItemClickListener? = null

    interface ItemClickListener {
        fun onItemClick(v: View, position: Int)
    }

    /*override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): VH {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_menu, parent, false)
        return VH(view)
    }*/

    override fun getItemCount(): Int = itemList.size

    //是否显示下拉选项图标
    fun showImg(b: Boolean) {
        this.isShow = b
    }

    //是否显示下拉子列表
    fun showChildList(b: Boolean) {
        this.isShowChild = b
    }

    /*override fun onBindViewHolder(holder: VH?, position: Int) {
        holder?.itemTitleTextView?.text = itemList[position].itemTitle
//        holder?.itemIconImgView?.imageResource = itemList[position].itemIcon
        holder?.itemLinear?.setOnClickListener {
            itemClickListener?.onItemClick(position)
        }
    }*/

    class VH internal constructor(view: View) : RecyclerView.ViewHolder(view) {
        var itemLinear = view.findViewById<LinearLayout>(R.id.item_linear)
        var child_item_list = view.findViewById<RecyclerView>(R.id.child_item_list)
        var itemIconImgView = view.findViewById<ImageView>(R.id.item_img_icon)
        var itemTitleTextView = view.findViewById<TextView>(R.id.item_txt_title)
    }
}

